package net.lab1024.sa.admin.module.flowable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.flowable.domain.FlowableDeploy;
import net.lab1024.sa.admin.module.flowable.page.TableDataInfo;
import net.lab1024.sa.admin.module.flowable.service.IFlowableDeployService;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程部署Controller
 *
 * @author ruoyi
 * @date 2023-12-18
 */
@RestController
@RequestMapping("/flowable/deploy")
public class FlowableDeployController extends BaseController
{
    @Resource
    private IFlowableDeployService flowableDeployService;

    /**
     * 查询流程部署列表
     */
    @SaCheckPermission("flowable:deploy:list")
    @GetMapping("/list")
    public ResponseDTO<TableDataInfo> list(FlowableDeploy flowableDeploy)
    {
        startPage();
        List<FlowableDeploy> list = flowableDeployService.selectFlowableDeployList(flowableDeploy);
        return ResponseDTO.ok(getDataTable(list));
    }

    /**
     * 获取流程部署详细信息
     */
    @SaCheckPermission("flowable:deploy:query")
    @GetMapping(value = "/{definitionId}")
    public ResponseDTO<FlowableDeploy> getInfo(@PathVariable("definitionId") String definitionId)
    {
        return ResponseDTO.ok(flowableDeployService.selectFlowableDeployByDefinitionId(definitionId));
    }


    /**
     * 删除流程部署
     */
    @SaCheckPermission("flowable:deploy:remove")
	@DeleteMapping("/{definitionIds}")
    public ResponseDTO<Integer> remove(@PathVariable String[] definitionIds)
    {
        return ResponseDTO.ok(flowableDeployService.deleteFlowableDeployByDefinitionIds(definitionIds));
    }
    /**
     * 查询流程部署版本列表
     */
    @SaCheckPermission("flowable:deploy:publishList")
    @GetMapping("/publishList")
    public ResponseDTO<TableDataInfo> publishList(@RequestParam String processKey) {
        startPage();

        List<FlowableDeploy> list = flowableDeployService.queryPublishList(processKey);
        Long publishCount = flowableDeployService.getPublishCount(processKey);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setTotal(publishCount);
        return ResponseDTO.ok(dataTable);
    }

    /**
     * 激活或挂起流程
     *
     * @param state        状态（active:激活 suspended:挂起）
     * @param definitionId 流程定义ID
     */
    @SaCheckPermission("flowable:deploy:state")
    @PutMapping(value = "/changeState")
    public ResponseDTO<Object> changeState(@RequestParam String state, @RequestParam String definitionId) {
        flowableDeployService.updateState(definitionId,state);
        return ResponseDTO.ok();
    }

    /**
     * 读取xml文件
     *
     * @param definitionId 流程定义ID
     * @return
     */
    @SaCheckPermission("flowable:deploy:bpmnXml")
    @GetMapping("/bpmnXml/{definitionId}")
    public ResponseDTO<String> getBpmnXml(@PathVariable(value = "definitionId") String definitionId) {
        return ResponseDTO.ok(flowableDeployService.queryBpmnXmlById(definitionId));
    }
}
